package com.web.base.log.util;

import com.blue.core.util.NumberUtil;
import com.blue.integration.serve.Bucket;
import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.web.base.log.message.UploadRequest;
import com.web.base.log.message.UploadResponse;
import com.web.base.log.model.Upload;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 上传文件日志转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年11月21日
 */
public class UploadConvertor
{
	private UploadConvertor()
	{
	}
	
	public static Upload toUpload(UploadRequest request)
	{
		if (request == null)
			return null;
		
		Upload upload = new Upload();
		upload.setId(request.getId());
		upload.setUserId(request.getUserId());
		upload.setName(request.getName());
		upload.setKey(request.getKey());
		upload.setSize(request.getSize());
		upload.setType(request.getType());
		upload.setMimeType(request.getMimeType());
		upload.setUploadTime(request.getUploadTime());
		return upload;
	}
	
	public static List<Upload> toUpload(List<UploadRequest> requestList)
	{
		List<Upload> list = new ArrayList<>();
		if (requestList == null || requestList.isEmpty())
			return list;
		
		for (UploadRequest request : requestList)
		{
			list.add(toUpload(request));
		}
		return list;
	}
	
	public static UploadResponse toResponse(Upload upload, StorageClient storageClient)
	{
		if (upload == null)
			return null;
		
		UploadResponse response = new UploadResponse();
		response.setId(upload.getId());
		response.setUserId(upload.getUserId());
		response.setName(upload.getName());
		response.setKey(upload.getKey());
		response.setSize(upload.getSize());
		response.setType(upload.getType());
		response.setMimeType(upload.getMimeType());
		response.setUploadTime(upload.getUploadTime());

		response.setUserName(upload.getUserName());
		response.setSizeName(NumberUtil.byteFormat(upload.getSize().longValue()));
		response.setUrl(storageClient.getUrl(Bucket.FILE, response.getKey()));
		return response;
	}
	
	public static List<UploadResponse> toResponse(List<Upload> list, StorageClient storageClient)
	{
		List<UploadResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (Upload upload : list)
		{
			responseList.add(toResponse(upload, storageClient));
		}
		return responseList;
	}
	
	public static Map<Integer, UploadResponse> toResponse(Map<Integer, Upload> map, StorageClient storageClient)
	{
		Map<Integer, UploadResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, Upload> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue(), storageClient));
		}
		return responseMap;
	}
	
	public static Page<UploadResponse> toResponse(Page<Upload> page, StorageClient storageClient)
	{
		if (page == null)
			return null;
		
		Page<UploadResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList(), storageClient));
		return responsePage;
	}
	
}
